package com.wyg.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet Filter implementation class EncodFilter
 */
@WebFilter("/EncodFilter")
public class EncodingFilter implements Filter {

	private FilterConfig config = null;
	private String encoding = "";
	public EncodingFilter() {
		// TODO Auto-generated constructor stub
		System.out.println("EncodingFilter");
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		
		request.setCharacterEncoding(this.encoding);
		response.setCharacterEncoding(this.encoding);
		
//		java.io.PrintWriter out = res.getWriter();
//		res.setContentType("text/html;charset=UTF-8");
//		out.print("过滤器设置编码为："+this.encoding +"");
		
		chain.doFilter(req,res);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		this.config = config;
		//this.encoding = config.getInitParameter("encoding");
		this.encoding="utf-8";
	}

}
